home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / OLESTREA.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  5KB  |  188 lines

  1. /*
  2.  * OLESTREA.C
  3.  *
  4.  * Constructor, Destructor, and Methods for the STREAM structure.  The
  5.  * methods are called from OLECLI.DLL through the LPOLESTREAMVTBL at
  6.  * the beginning of the STREAM structure.
  7.  *
  8.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  9.  *
  10.  */
  11.  
  12. #include <windows.h>
  13. #include <ole.h>
  14. #include "oclient.h"
  15.  
  16.  
  17.  
  18. /*
  19.  * PStreamAllocate
  20.  *
  21.  * Purpose:
  22.  *  Allocates and initializes a STREAM structure, using the function
  23.  *  PVtblStreamAllocate to initialize its VTBL.  If pfnGet is NULL,
  24.  *  we use the StreamGet method in OLESTREA.C.  Likewise, if pfnPut is
  25.  *  NULL, we use the StreamPut method in OLESTREA.C.
  26.  *
  27.  * Parameters:
  28.  *  pfSuccess       LPBOOL indicating if the initialization succeeded.  If
  29.  *                  this function returns non-NULL, but *pfSuccess==FALSE,
  30.  *                  the caller must call the destructor function.
  31.  *  hInst           HANDLE of the application instance.
  32.  *  pfnGet          FARPROC to the stream's Get method.
  33.  *  pfnPut          FARPROC to the stream's Put method.
  34.  *
  35.  * Return Value:
  36.  *  LPSTREAM        Pointer to the allocated STREAM if successful, NULL
  37.  *                  if the allocation failed or a parameter is invalid.
  38.  */
  39.  
  40. LPSTREAM FAR PASCAL PStreamAllocate(LPBOOL pfSuccess, HANDLE hInst,
  41.                                     FARPROC pfnGet, FARPROC pfnPut)
  42.     {
  43.     HANDLE          hMem;
  44.     LPSTREAM        pStream;
  45.  
  46.     if (NULL==pfSuccess)
  47.         return NULL;
  48.  
  49.     *pfSuccess=FALSE;
  50.  
  51.     if (NULL==pfnGet)
  52.         pfnGet=(FARPROC)StreamGet;
  53.  
  54.     if (NULL==pfnPut)
  55.         pfnGet=(FARPROC)StreamPut;
  56.  
  57.  
  58.     hMem=LocalAlloc(LPTR, CBSTREAM);
  59.  
  60.     if (NULL==hMem)
  61.         return FALSE;
  62.  
  63.     //All fields are initially NULL from the LocalAlloc.
  64.     pStream=(LPSTREAM)(PSTR)hMem;
  65.  
  66.     //Get initialized OLESTREAMVTBL pointer.  Note that this sets pfSuccess.
  67.     pStream->pvt=PVtblStreamAllocate(pfSuccess, hInst, pfnGet, pfnPut);
  68.     return pStream;
  69.     }
  70.  
  71.  
  72.  
  73. /*
  74.  * PStreamFree
  75.  *
  76.  * Purpose:
  77.  *  Frees all data in the STREAM and frees the structure.
  78.  *
  79.  * Parameters:
  80.  *  pStream         LPSTREAM to the structure to free.
  81.  *
  82.  * Return Value:
  83.  *  LPSTREAM        NULL if the function succeeds, pStream otherwise.
  84.  */
  85.  
  86. LPSTREAM FAR PASCAL PStreamFree(LPSTREAM pStream)
  87.     {
  88.     if (NULL==pStream)
  89.         return NULL;
  90.  
  91.     //Free this object's VTBL
  92.     if (NULL!=PVtblStreamFree(pStream->pvt))
  93.         return pStream;
  94.  
  95.     if (NULL!=LocalFree((HANDLE)(DWORD)pStream))
  96.         return pStream;
  97.  
  98.     return NULL;
  99.     }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. /*
  109.  * StreamGet
  110.  *
  111.  * Purpose:
  112.  *  Instructs the client to read a specified number of bytes from its
  113.  *  already open file.  This method must be capable of handling data that
  114.  *  is greater than 64K, which is why we use the DwReadHuge function
  115.  *  in fileio.c.
  116.  *
  117.  * Parameters:
  118.  *  pStream         LPSTREAM to the stream structure holding the file handle.
  119.  *  pb              LPBYTE in which to read the data.  We have no idea what
  120.  *                  data we'll read since it's native server data.
  121.  *  cb              DWORD number of bytes to read from the file into pb.
  122.  *
  123.  * Return Value:
  124.  *  DWORD           Number of bytes actually read.
  125.  */
  126.  
  127. DWORD FAR PASCAL StreamGet(LPSTREAM pStream, LPBYTE pb, DWORD cb)
  128.     {
  129.     DWORD       cbRead;
  130.  
  131.     /*
  132.      * If you already have a file handle in the STREAM structure,
  133.      * then just read cb bytes of the data into pb from that file handle.
  134.      * This assumes that we are in the process of reading a file and
  135.      * have an open file handle.
  136.      */
  137.  
  138.     if (NULL==pStream->hFile)
  139.         return 0L;
  140.  
  141.     cbRead=DwReadHuge(pStream->hFile, (LPVOID)pb, cb);
  142.  
  143.     //If cb!=cbRead, OLECLI will consider it an error.  No need to check here.
  144.     return cbRead;
  145.     }
  146.  
  147.  
  148.  
  149.  
  150. /*
  151.  * StreamPut
  152.  *
  153.  * Purpose:
  154.  *  Instructs the client to write a specified number of bytes to its
  155.  *  already open file.  This method must be capable of handling data that
  156.  *  is greater than 64K, which is why we use the DwWriteHuge function
  157.  *  in fileio.c.
  158.  *
  159.  * Parameters:
  160.  *  pStream         LPSTREAM to the stream structure holding the file handle.
  161.  *  pb              LPBYTE to the data to write.  We have no idea what
  162.  *                  data sits here.
  163.  *  cb              DWORD number of bytes to write from pb.
  164.  *
  165.  * Return Value:
  166.  *  DWORD           Number of bytes actually written.
  167.  */
  168.  
  169. DWORD FAR PASCAL StreamPut(LPSTREAM pStream, LPBYTE pb, DWORD cb)
  170.     {
  171.     DWORD       cbWritten;
  172.  
  173.     /*
  174.      * If you already have a file handle in the STREAM structure,
  175.      * then just write cb bytes of the data at pb to that file handle.
  176.      * This assumes that we are in the process of writing a file and
  177.      * have an open file handle.
  178.      */
  179.  
  180.     if (NULL==pStream->hFile)
  181.         return 0L;
  182.  
  183.     cbWritten=DwWriteHuge(pStream->hFile, (LPVOID)pb, cb);
  184.  
  185.     //If cb!=cbWritten, OLECLI will consider it an error.  No need to check here.
  186.     return cbWritten;
  187.     }
  188.